package org.testis.guiappsmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.guiappsmodule.data.GuiAppsModuleDataObject;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.utils.OsInfo;


/**
 *
 * @author nurhat
 */
public class GuiAppsExecuteAction extends AtofModuleAction
{

    private OsExecuteAction executionImpl;


    public GuiAppsExecuteAction()
    {
        if (OsInfo.isWindows()) {
            executionImpl = new WindowsExecuteAction();
        }
        else if (OsInfo.isLinux()) {
            executionImpl = new LinuxExecuteAction();
        }
        else {
            throw new TestStepException(TestStepResultType.FAILURE,
                                        "Running on unsupported OS at GuiAppsExecuteAction...");
        }
    }


    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        boolean result = false;
        GuiAppsModuleDataObject dataObj =
            (GuiAppsModuleDataObject) request.getDataObj();
        try {
            result = executionImpl.executeGuiScript(dataObj);
        } catch (GuiAppsException ex) {
            if (ex.getResultType() == GuiAppsExceptionType.INTERRUPTED_EXCEPTION) {
                throw new TestStepException(TestStepResultType.FAILURE,
                                            ex.getMessage() +
                                            " at GuiAppsExecuteAction");
            }
            else if (ex.getResultType() ==
                GuiAppsExceptionType.UNSUPPORTED_OS) {
                throw new TestStepException(TestStepResultType.FAILURE,
                                            ex.getMessage() +
                                            " at GuiAppsExecuteAction");
            }
            else if (ex.getResultType() ==
                GuiAppsExceptionType.IO_EXCEPTION) {
                throw new TestStepException(TestStepResultType.FAILURE,
                                            ex.getMessage() +
                                            " at GuiAppsExecuteAction");
            }
        }
        return result;
    }
}
